package com.cqxyd.smartsite.util.v2;

import com.cqxyd.smartsite.pojo.v2.returnInfo.UploadResult;
import com.cqxyd.smartsite.util.JSONResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author Angus Yu
 * @date 2020-10-20 17:17
 */
@Service
public class TowerCranePostHelper {
    private final Logger logger = LoggerFactory.getLogger(TowerCranePostHelper.class);

    @Autowired
    private BasicPostHelper basicPostHelper;

    @Value("${tower_crane_basic_url}")
    private String tower_crane_basic_url;
    @Value("${tower_crane_driver_url}")
    private String tower_crane_driver_url;
    @Value("${tower_crane_real_time_url}")
    private String tower_crane_real_time_url;
    @Value("${tower_crane_work_cycle_url}")
    private String tower_crane_work_cycle_url;
    @Value("${tower_crane_location_url}")
    private String tower_crane_location_url;
    @Value("${tower_crane_climbing_url}")
    private String tower_crane_climbing_url;

    /**
     * 塔吊监测基础信息接口
     * @param basicInfo 基础信息
     * @return
     */
    public JSONResult<UploadResult> basic(String basicInfo){
        UploadResult uploadResult;
        ObjectMapper mapper = new ObjectMapper();
        JSONResult<String> postResult = basicPostHelper.postRequest(tower_crane_basic_url,basicInfo);
        if (postResult.isOK())
        {
            try {
                uploadResult = mapper.readValue(postResult.getData(), UploadResult.class);
            } catch (JsonMappingException e) {
                logger.error(e.getMessage());
                e.printStackTrace();
                return JSONResult.errorMsg(e.getMessage());
            } catch (JsonProcessingException e) {
                logger.error(e.getMessage());
                e.printStackTrace();
                return JSONResult.errorMsg(e.getMessage());
            }
        }else{
            return JSONResult.errorMsg(postResult.getMsg());
        }
        return JSONResult.ok(uploadResult);
    }
}
